<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\Config;

class Login extends Base
{
    public function index()
    {
        if (session('adminSessionData')){
            return redirect('/admin/index');
        }
        if (request()->isPost()){
            $data = input('post.');
            if(!captcha_check($data['verifycode'])){
                // 验证失败
                return alert('验证码错误','index',5);
            }
            // 用户名
            $adminData = Db::name('admin')->where('username',$data['username'])->find();
            if (!$adminData){
                return alert('用户不存在','index',5);
            }
            if ($adminData['status'] == 0){
                return alert('账户被禁用','index',5);
            }
            // 密码
            if ($adminData['password'] != $this->password_salt($data['password'])){
                return alert('用户名或者密码错误不存在','index',5);
            }
            Db::name('admin')->where('id',$adminData['id'])->update(['last_login_time' => time()]);
            session('adminSessionData',$adminData);
            return alert('登录成功','/admin/index/index',6);
        }
        return view();
    }

}
